﻿@using KPIS.GERP.Models.ViewModel
@model IEnumerable<KPIS.GERP.Models.ICM.DeliveryNotePenalty>

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "รายการตรวจรับ";
    ViewBag.Descr = "ข้อมูลรายการค่าปรับ";

    int DnId = (int)ViewBag.DnId;
    int PoId = ((int?)ViewBag.PoId) ?? 0;

    var service = new KPIS.GERP.WEB.GIMS.DeliveryNoteService.DeliveryNoteServiceClient();
    var PoModel = service.SelectDeliveryNote(new KPIS.GERP.Models.ICM.DeliveryNote { PoId = PoId, DnId = DnId, RecordStatus = "!D" }, new[] { "PurchaseOrder" });

}


@using (Html.BeginForm("DeleteAll", "DeliveryNotePenal", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    @*แจ้งบันทึกรายการปรับ รายละเอียดแจ้งเพื่อทำการปรับ*@
    <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
        <thead>
            <tr>
                <th>กำหนดส่งมอบ</th>
                <th>วันตรวจรับ</th>
                <th>ผลลัพธ์การส่งมอบ</th>
                <th>ราคาสุทธิ</th>
            </tr>
        </thead>

        <tbody>
            @foreach (var m in PoModel)
            {
                <tr>

                    <td>@Html.DisplayFor(model => m.PurchaseOrder.DeliveryDate) </td>
                    <td>@Html.DisplayFor(model => m.ReceiveDate)</td>


                    @{
                DateTime PoDelivery = (DateTime)m.PurchaseOrder.DeliveryDate;
                DateTime ReceiveDate = (DateTime)m.ReceiveDate;
                //TimeSpan? Date = PoDelivery - ReceiveDate;
                int D = PoDelivery.Subtract(ReceiveDate).Days;
                if (D < 0)
                {
                    ViewBag.date = "เกินกำหนด";
                    ViewBag.Display = "normal";
                    <td style="color:red">@ViewBag.date</td>
                }
                else
                {
                    ViewBag.date = "ภายในกำหนด";
                    ViewBag.Display = "none";
                    <td style="color:green">@ViewBag.date</td>
                }
                    }

                    <td>@Html.DisplayFor(model => m.PurchaseOrder.PrepayAmount)</td>

                </tr>
            }
        </tbody>
    </table>


    <div id="dvDnPenalty"> @*style="display:@ViewBag.Display;">*@
        <div class="block-button-menu-above">
            @*@Html.ButtonAdd()*@

            @*@Html.ActionLink("เพิ่ม", "Create", new { DnId=@Model.FirstOrDefault().DnId ,PoId=@Model.FirstOrDefault().DeliveryNote.PoId}
                , new { @class="btn btn-small btn-success button-standard-width", role="button" })*@
            @if ( 0 == 0 )//ViewBag.ErrorMessage != null)
            {
                @Html.ActionLink("เพิ่ม", "Create", new { DnId = ViewData["DnId"], PoId = ViewData["PoId"] }
                , new { @class = "btn btn-small btn-success button-standard-width", role = "button" })
            }


            @*@Html.ButtonDeleteAll()*@
        </div>
        @*รายการที่ต้องปรับ*@
        <div class="row-fluid">

            @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

            <div class="row-fluid">
                <div class="table-header span12">
                    <div class="span10">
                        @ViewBag.Descr
                    </div>
                    @Html.Partial("_ExportButton")
                </div>
            </div>

            <table id="table1" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
                <thead>
                    <tr>
                        <th> กำหนดการปรับ</th>
                        <th>รายละเอียด</th>
                        <th>ค่าปรับจากการคำนวณ</th>
                        <th>ค่าปรับจริง</th>
                        <th>หมายเหตุ</th>
                        <th></th>
                    </tr>
                </thead>

                <tbody>
                    @foreach (var m in Model)
                    {
                        <tr>

                            @*<td>@Html.ActionLink(@m.PurchaseOrderPenalty.Name, "Details", new { PoPenId = m.PoPenId, DnId = m.DnId }) </td>*@
                            <td>@Html.DisplayFor(model => m.PurchaseOrderPenalty.Name)</td>
                            <td>@Html.DisplayFor(model => m.PurchaseOrderPenalty.Descr)</td>
                            <td>@Html.DisplayFor(model => m.PenaltyCalculate)</td>
                            <td>@Html.DisplayFor(model => m.PenaltyFinalAmount)</td>
                            <td>@Html.DisplayFor(model => m.Remarks)</td>


                            <td class="td-actions">
                                <div class="hidden-phone visible-desktop btn-group">
                                    @Html.ButtonDetails(new { DnPenId = m.DnPenId }, true)
                                    @Html.ButtonEdit(new { DnPenId = m.DnPenId }, true)
                                    @Html.ButtonDelete(new { DnPenId = m.DnPenId, DnId = m.DnId }, true)@*, PoId=m.PoId, PoInstallId=m.PoInstallId*@
                                </div>
                            </td>

                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
}
@section scripts {

    <script type="text/javascript">

        //$(function () {
        //	var oTable1 = $('#table').dataTable({
        //		"aoColumns": [
        //            { "bSortable": false },
        //            null,
        //            null,
        //            null,
        //            null,

        //            { "bSortable": false }
        //		]
        //	});

        $('table th input:checkbox').on('click', function () {
            var that = this;
            $(this).closest('table').find('tr > td:first-child input:checkbox')
            .each(function () {
                this.checked = that.checked;
                $(this).closest('tr').toggleClass('selected');
            });

        });

        $('[data-rel=tooltip]').tooltip();

        $("form").submit(function () {
            var currentForm = this;
            bootbox.confirm(function (result) {
                var _result = result;
                if (result) {
                    $.ajax({
                        url: '@(Url.Action("DeleteAll", "DeliveryNotePenalty"))',
                        type: "post",
                        dataType: "json",

                        data: { DnId: DnId, PoId: PoId },
                        success: function (result) {
                            if (result.success) {
                                location.reload();
                            }
                            else {
                            }
                        }
                    });
                }
            })
            return false;

        });

    </script>
}

